home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / dev / e / yaec.lha / examples / csv-norm.e < prev    next >
Text File  |  2001-08-12  |  1KB  |  45 lines

  1. /* A suitably large size for the record buffer */
  2. CONST BUFFERSIZE=512
  3.  
  4. -> yaec-note : replaced some " inside strings with \q.
  5.  
  6. PROC main()
  7.   DEF filehandle, status, buffer[BUFFERSIZE]:STRING, filename
  8.   filename:='datafile'
  9.   IF filehandle:=Open(filename, OLDFILE)
  10.     REPEAT
  11.       status:=ReadStr(filehandle, buffer)
  12.       /* This is the way to check ReadStr() actually read something */
  13.       IF buffer[] OR (status<>-1) THEN process_record(buffer)
  14.     UNTIL status=-1
  15.     /* If Open() succeeded then we must Close() the file */
  16.     Close(filehandle)
  17.   ELSE
  18.     WriteF('Error: Failed to open \q\s\q\n', filename)
  19.   ENDIF
  20. ENDPROC
  21.  
  22. PROC process_record(line)
  23.   DEF i=1, start=0, end, s
  24.   /* Show the whole line being processed */
  25.   WriteF('Processing record: \q\s\q\n', line)
  26.   REPEAT
  27.     /* Find the index of a comma after the start index */
  28.     end:=InStr(line, ',', start)
  29.     /* If a comma was found then terminate with a NIL */
  30.     IF end<>-1 THEN line[end]:=NIL
  31.     /* Point to the start of the field */
  32.     s:=line+start
  33.     IF s[]
  34.       /* At this point we could do something useful... */
  35.       WriteF('\t\d) \q\s\q\n', i, s)
  36.     ELSE
  37.       WriteF('\t\d) Empty Field\n', i)
  38.     ENDIF
  39.     /* The new start is after the end we found */
  40.     start:=end+1
  41.     i++
  42.   /* Once a comma is not found we've finished */
  43.   UNTIL end=-1
  44. ENDPROC
  45.